package com.boot.redis.service;

import com.boot.redis.mapper.TestDataMapper;
import com.boot.redis.model.TestData;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service("testDateService")
public class TestDateService {

    @Autowired
    private TestDataMapper testDataMapper;


    @Cacheable(value="mycache",key = "'allTestData'")
    public List<TestData> getTestDateList(){

        PageHelper.startPage(1,20);
        List<TestData> testData = testDataMapper.selectAll();

        return testData;

    }


    @Cacheable(value="mycache",key = "#page + #size")
    public List<TestData> getTestDateListPage(Integer page,Integer size){

        PageHelper.startPage(page,size);
        List<TestData> testData = testDataMapper.selectAll();

        return testData;

    }


    @CacheEvict(value="mycache",allEntries=true)
    public void saveTestData(TestData testData){
        testDataMapper.insert(testData);
    }


    @CacheEvict(value="mycache",key = "#testData.id")
    public void deleteTestDataById(TestData testData){

        Example example = new Example(TestData.class);
        example.createCriteria().andEqualTo("id",testData.getId());
        testDataMapper.deleteByExample(example);

    }

    @CacheEvict(value="mycache",key = "#testData.id")
    public void updateTestDataById(TestData testData){
        Example example = new Example(TestData.class);
        example.createCriteria().andEqualTo("id",testData.getId());
        testDataMapper.updateByExampleSelective(testData,example);

    }

}
